package com.xjy.rpc.proxy;

import com.xjy.rpc.proxy.cglib.CglibProxyFactory;
import com.xjy.rpc.proxy.jdk.JdkProxyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: TODO 动态代理工厂类  静态代码块手动注入或springboot注入map
 * @author XJY
 * @date 2024/9/30 9:10
 * @version 1.0
 */
@Component
public class ProxyFactory {

    //代理方法的map
    @Autowired //使用注入注解可利用springboot直接注入 类名->对象实例
    private static Map<String,IProxy> proxyMap = new ConcurrentHashMap<>();

    {
        //这里可以手动注入
        if (proxyMap.isEmpty()){
            //如果为null执行容错方法
            proxyMap.clear();
            proxyMap.put("CGLIB",new CglibProxyFactory());
            proxyMap.put("JDK",new JdkProxyFactory());
        }
    }
    //默认使用CGLIB代理
    public static IProxy getProxy(String proxyName){
        return proxyMap.getOrDefault(proxyName,new CglibProxyFactory());
    }

}
